package com.ctrl.riveradministrator.mvp.view.activity.scenery_share;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.LearnZoom;
import com.ctrl.riveradministrator.mvp.model.SceneryShare;
import com.ctrl.riveradministrator.mvp.view.weight.ImageViewActivity;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class SceneryDetailActivity extends BaseActivity {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private SceneryShare sceneryShare;
    private TextView jjTv, xxTv;
    private VideoView videoView;
    private SimpleDraweeView imageView;
    private GenericDraweeHierarchyBuilder builder;
    private GenericDraweeHierarchy hierarchy;
    private ImageRequest request;
    private DraweeController controller;
    private Uri uri;

    @Override
    protected void initParams(Bundle parms) {
        sceneryShare = (SceneryShare) getIntent().getSerializableExtra("sceneryShare");
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_scenery_detail;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(R.id.toolbar);
        titleTv = (TextView) $(R.id.title_tv);
        leftMenu = (ImageView) $(R.id.left_iv);

        imageView = (SimpleDraweeView) $(R.id.b_im);
        jjTv = (TextView) $(R.id.title);
        xxTv = (TextView) $(R.id.type);
        videoView = (VideoView) $(R.id.video);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(sceneryShare.getShareBiaoti());
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }

    @Override
    protected void doBusiness(Context mContext) {
        jjTv.setText(sceneryShare.getShareContent());
        xxTv.setText(sceneryShare.getShareDetalis());
        Uri uri = Uri.parse(Constants.BASE_URL_ + sceneryShare.getShareDetalis());
        hierarchy = imageView.getHierarchy();
        hierarchy.setFailureImage(R.mipmap.ic_launcher);
        hierarchy.setProgressBarImage(new ProgressBarDrawable());
        //imageView.setImageURI(uri);
        if (sceneryShare.getShareDetalis() == null || sceneryShare.getShareDetalis().equals("")) {
            return;
        }
        if (sceneryShare.getFieType().equals("1")) {
            request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(100, 100))
                    .build();
            controller = Fresco.newDraweeControllerBuilder()
                    .setOldController(imageView.getController())
                    .setImageRequest(request)
                    .build();
            imageView.setController(controller);
        } else if (sceneryShare.getFieType().equals("2")) {
            videoView.setVisibility(View.VISIBLE);
            imageView.setVisibility(View.GONE);
            videoView.setVideoURI(uri);
            MediaController mediaController = new MediaController(this);
            videoView.setMediaController(mediaController);
            videoView.requestFocus();
        } else {
            jjTv.setText(Html.fromHtml(sceneryShare.getShareDetalis()));
            imageView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.b_im:
                Intent intent = new Intent(this, ImageViewActivity.class);
                intent.putExtra("img_url", Constants.BASE_URL_ + sceneryShare.getShareDetalis());
                intent.putExtra("type", "net");
                startActivity(intent);
                break;
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        imageView.setOnClickListener(this);
        leftMenu.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
